<?php
/**
 * Static content controller.
 *
 * This file will render views from views/pages/
 *
 * PHP 5
 *
 * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
 * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
 *
 * Licensed under The MIT License
 * For full copyright and license information, please see the LICENSE.txt
 * Redistributions of files must retain the above copyright notice.
 *
 * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
 * @link          http://cakephp.org CakePHP(tm) Project
 * @package       app.Controller
 * @since         CakePHP(tm) v 0.2.9
 * @license       MIT License (http://www.opensource.org/licenses/mit-license.php)
 */
App::uses('AppController', 'Controller');

/**
 * Static content controller
 *
 * Override this controller by placing a copy in controllers directory of an application
 *
 * @package       app.Controller
 * @link http://book.cakephp.org/2.0/en/controllers/pages-controller.html
 */
class DepartmentsController extends AppController {
	
	public $helpers = array('Html', 'Form');
	
	public function beforeFilter() {
		parent::beforeFilter();
	}
	
	public function index(){
		if ($this->Session->read('Auth.User.role') === 'superadmin') {
			$this->set('departments', $this->Department->find('all'));
		}else{//deny access
			$this->redirect(array('controller' => 'timesheets', 'action' => 'index'));
		}
		
	}
	
	public function view($id = null) {
		if ($this->Session->read('Auth.User.role') === 'superadmin') {
			if (!$id) {
				throw new NotFoundException(__('Invalid client'));
			}
	
			$client = $this->Client->findById($id);
			if (!$client) {
				throw new NotFoundException(__('Invalid client'));
			}
			$this->set('client', $client);
		}else{//deny access
			$this->redirect(array('controller' => 'timesheets', 'action' => 'index'));
		}
    }
	
	public function add() {
		if ($this->Session->read('Auth.User.role') === 'superadmin') {
			if ($this->request->is('post')) {
				$this->request->data['Department']['created_by'] = $this->Auth->user('id'); //Added this line
				if ($this->Department->save($this->request->data)) {
					$this->Session->setFlash('Your department has been saved.');
					$this->redirect(array('action' => 'index'));
				}
			}
		}else{//deny access
			$this->redirect(array('controller' => 'timesheets', 'action' => 'index'));
		}
		

	}
	
	public function edit($id = null) {
		if ($this->Session->read('Auth.User.role') === 'superadmin') {
			if (!$id) {
				throw new NotFoundException(__('Invalid client'));
			}
		
			$department = $this->Department->findById($id);
			if (!$department) {
				throw new NotFoundException(__('Invalid client'));
			}
		
			if ($this->request->is('department') || $this->request->is('put')) {
				$this->Department->id = $id;
				if ($this->Department->save($this->request->data)) {
					$this->Session->setFlash('Your department has been updated.');
					$this->redirect(array('action' => 'index'));
				} else {
					$this->Session->setFlash('Unable to update your department.');
				}
			}
	
			if (!$this->request->data) {
				$this->request->data = $department;
			}
		
		}else{//deny access
			$this->redirect(array('controller' => 'timesheets', 'action' => 'index'));
		}
	}
	
	public function delete($id) {
		if ($this->Session->read('Auth.User.role') === 'superadmin') {
			if ($this->request->is('get')) {
				throw new MethodNotAllowedException();
			}
		
			if ($this->Department->delete($id)) {
				$this->Session->setFlash('The department with id: ' . $id . ' has been deleted.');
				$this->redirect(array('controller' => 'departments', 'action' => 'index'));
			}
		}else{//deny access
			$this->redirect(array('controller' => 'timesheets', 'action' => 'index'));
		}
	}
}
